﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace SaaS
{
    public partial class WorkflowExecute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoginID"] == null)
                Response.Redirect("UserLogin.aspx");
            else if (lblWorkflowID.Text.Length <= 0)
            {
                lblWorkflowID.Text  = Session["WorkflowID"].ToString();
                lblWorkflowName.Text = Session["WorkflowName"].ToString();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string argument = txtInput.Text.Trim();
            WebServiceInvoker invoker;
            string service;
            string method;
            string path;
            string[] args;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                path = GridView1.Rows[i].Cells[4].Text;
                if (path[0] == '/')
                {
                    path = Request.Url.Scheme +"://"+ Request.Url.Authority + path;
                }
                 invoker = new WebServiceInvoker(new Uri(path));
                 service = GridView1.Rows[i].Cells[2].Text;
                 method = GridView1.Rows[i].Cells[3].Text;
                 if (argument.Equals(""))
                    args = new string[] { argument };
                 else
                    args = argument.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
                 argument = invoker.InvokeMethod<string>(service, method, args);
                 GridView1.Rows[i].Cells[6].Text = argument;
            }
            txtOutput.Text = argument;
        }
    }
}
